import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {HttpServicesProvider} from "../../providers/http-services/http-services";
import {ConfigProvider} from "../../providers/config/config";
import {ValidateProvider} from "../../providers/validate/validate";
import {StorageProvider} from "../../providers/storage/storage";
import {UtilsProvider} from "../../providers/utils/utils";

@IonicPage({
    segment: 'service/talent_pool',
    defaultHistory: ['HomePage', 'ServicePage', 'TalentPoolSearchPage']
})
@Component({
    selector: 'page-talent-pool',
    templateUrl: 'talent-pool.html',
})
export class TalentPoolPage {
    resumeList = [];
    resumeTotal:number = 0;
    page:number = 1;
    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public httpService: HttpServicesProvider,
        public config: ConfigProvider,
        public validate: ValidateProvider,
        public storage: StorageProvider,
        public util: UtilsProvider
    ) {
        if (!this.validate.checkLogin()) {
            this.navCtrl.push('LoginPage');
        }
    }

    ionViewCanEnter(){
        return this.validate.checkLogin();
    }

    ionViewDidLoad() {
        this.getData(null);
    }

    getData(infiniteScroll){
        let postData = {
            token: this.config.getToken(),
            page: this.page
        };
        if(this.storage.get('resumeSearchData')){
            Object.assign(postData,JSON.parse(this.storage.get('resumeSearchData')));
        }
        let apiUrl = "/talents/resume_list";
        this.httpService.requestPostData(apiUrl, postData, (response)=>{
            if(response.error == 200) {
                this.resumeList = this.resumeList.concat(response['data']['lists']);
                this.resumeTotal = response['data']['total'];
                this.page++;
                if (infiniteScroll) {
                    infiniteScroll.complete();
                    /*请求完成数据更新页面*/
                    if (response['data']['lists'].length < response['data']['page_size']) {   /*没有数据 禁用上拉更新*/
                        infiniteScroll.enable(false);
                    }
                }
            }
        })
    }

    doInfinite(infiniteScroll) {
        this.getData(infiniteScroll);
    }

    ionViewWillLeave(){
        this.storage.remove('resumeSearchData');
    }

    resumeDetail(resume_id){
        this.httpService.requestPostData("/user_info/check_level",{
            token: this.config.getToken()
        }, (response)=>{
            if(response['error'] == 200 && response['reg_step'] == 2){
                //高级用户处理
                //查询是否为收费信息，是否已付费
                this.httpService.requestPostData("/talents/resume_detail", {
                    token: this.config.getToken(),
                    resume_id: resume_id
                }, data => {
                    if (data['error'] === 300) {
                        this.util.presentConfirm(data['message'], ()=>{
                            this.httpService.requestPostData('/talents/resume_pay', {
                                token: this.config.getToken(),
                                resume_id: resume_id
                            }, res=>{
                                if(res['error'] == 200){
                                    this.navCtrl.push('ResumeDetailPage', {resume_id: resume_id});
                                    return false;
                                }else{
                                    this.util.presentAlert(res['message']);
                                    return false;
                                }
                            })
                        });
                        return false;
                    } else if(data['error'] == 200){
                        this.navCtrl.push('ResumeDetailPage', {resume_id: resume_id});
                        return false;
                    } else {
                        this.util.presentAlert(data['message']);
                    }
                });
            }else{
                this.util.presentConfirm('该功能为高级用户权限，现在去完善注册信息？', ()=>{
                    this.navCtrl.push('RegisterPage');
                });
            }
        });

    }
}
